<html>

<style>
    * {
        margin: 0;
    }

    #content {
        color: white;
        position: absolute;
        left: -20px;
        top: -10px;
        white-space: pre-wrap;
        height: 200px;
        overflow: auto;
    }
</style>

<script type="importmap">
	{
	  "imports": {
		"three": "../../three.js-r151/build/three.module.js",
		"three/addons/": "../../three.js-r151/examples/jsm/"
	  }
	}
</script>

<body>
    <div id="content">
        
    </div>

    <div id="canvas"></div>

    <script type="module">
        import * as THREE from 'three';
        import { OrbitControls } from 'three/addons/controls/OrbitControls.js';

        // 1. 创建三大件
        // 1.1 三维场景Scene, 类似创建了个世界，三维世界
        const scene = new THREE.Scene();
        const geometry = new THREE.BoxGeometry(1, 1, 1);
        const material = new THREE.MeshBasicMaterial({ color: 'yellow' });
        const cube = new THREE.Mesh(geometry, material);
        // scene.add(cube);

        const cube1 = new THREE.Mesh(geometry, material);
        cube1.position.set(2, 0, 0)
        // scene.add(cube1);

        // 添加辅助坐标系
        const axesHelper = new THREE.AxesHelper(5);
        scene.add(axesHelper);

        // 1.2 相机
        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.set(5, 5, 5)

        // 1.3 渲染器
        const renderer = new THREE.WebGLRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);
        let canvasDom = document.getElementById("canvas");
        canvasDom.appendChild(renderer.domElement);

        // 设置相机控件轨道控制器OrbitControls
        new OrbitControls(camera, canvasDom);

        function render() {
            renderer.render(scene, camera);
            requestAnimationFrame(render)
        }
        render();


        // 2. 可以添加group进行分组
        const group = new THREE.Group();
        scene.add(group)

        group.add(cube)
        group.add(cube1)
        
        setTimeout(() => {
            // 组内所有元素整体挪动, 不分彼此
            group.translateY(2);
        }, 5000)

        // 所有的对象都是继承Object3D, add方法在它上面,所有mesh也能执行add方法
        const material2 = new THREE.MeshBasicMaterial({ color: 'green' });
        const cube2 = new THREE.Mesh(geometry, material2);
        cube2.position.z = 2
        // 这个是相对cube1的
        cube1.add(cube2)
        cube2.name = "sj"

        // 局部坐标系
        const meshAxesHelper = new THREE.AxesHelper(50);
        cube1.add(meshAxesHelper);

        // 3. 组件的查找方式
        group.traverse(obj => {
            if (obj.isMesh) {
                console.log("traverse", obj)
            }
        })

        console.log("find", scene.getObjectByName("sj"));

       

    </script>
</body>

</html>